package com.zcaxer.roomwordsample


import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.matcher.ViewMatchers.assertThat
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.zcaxer.roomwordsample.data.AppRoomDatabase
import com.zcaxer.roomwordsample.data.Building
import com.zcaxer.roomwordsample.data.BuildingDao
import org.hamcrest.CoreMatchers.equalTo
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.io.IOException

@RunWith(AndroidJUnit4::class)
class DatabaseTest {
    private lateinit var buildingDao: BuildingDao
    private lateinit var db: AppRoomDatabase

    @Before
    fun createDb() {
        val context = ApplicationProvider.getApplicationContext<Context>()
        db = Room.inMemoryDatabaseBuilder(context, AppRoomDatabase::class.java).build()
        buildingDao=db.buildingDao()

    }

    @After
    @Throws(IOException::class)
    fun closeDb(){
        db.close()
    }


    @Test
    @Throws(Exception::class)
    fun writeUserAndReadInList() {
        val building: Building = Building(0,"stoneProducer",-10,0.1f,2,3)
        buildingDao.insert(building)
        val a=buildingDao.getBuildings()
        assertThat(a.value!![0],equalTo(building))
    }
}